#!/usr/bin/env bash

source /usr/share/kwort/common/default

function list_items(){
	awk -F " " '$1 == "nameserver" {print "<item>"$2"</item>"}' /etc/resolv.conf
}

export ADD_DIALOG="<window title=\"Kwort Network Manager\" icon-name=\"${DNS_IMG}\" window_position=\"1\" resizable=\"false\">
	<vbox>
		<frame Gateway Settings>
			<hbox>
				<pixmap>
					<input file>${DNS_IMG}</input>
				</pixmap>
				<text>
					<label>New DNS: </label>
				</text>
				<entry>
					<variable>DNS</variable>
				</entry>
			</hbox>
		</frame>
		<hbox>
			<button cancel>
				<input file>${CANCEL_IMG}</input>
			</button>
			<button>
				<label>OK</label>
				<input file>${OK_IMG}</input>
				<action>echo nameserver \${DNS} >>/etc/resolv.conf</action>
				<action type=\"exit\">False exit</action>
			</button>
		</hbox>
	</vbox>
</window>"

while true; do
	MAIN_DIALOG="<window title=\"Kwort User Manager\" icon-name=\"${DNS_IMG}\" window_position=\"1\" resizable=\"false\">
	<vbox>
		<frame>
			<hbox>
				<vbox>
					<text use-markup=\"true\">
						<label>\"<b>DNS:</b>\"</label>
					</text>
					<text>
						<label>__________</label>
					</text>
					<pixmap>
						<input file>${DNS_IMG48}</input>
					</pixmap>
				</vbox>
				<list>
					$(list_items)
					<variable>DNS_REMOVE</variable>
				</list>
			</hbox>
		</frame>
		<hbox>
			<button>
				<label>Close</label>
				<input file>${CLOSE_IMG}</input>
				<action type=\"closewindow\">MAIN_DIALOG</action>
			</button>
			<button>
				<label>Add</label>
				<input file>${ADD_IMG}</input>
				<action type=\"launch\">ADD_DIALOG</action>
				<action type=\"closewindow\">MAIN_DIALOG</action>
			</button>
			<button>
				<label>Remove</label>
				<input file>${REMOVE_IMG}</input>
				<action>sed -i \"/nameserver[ ]*\${DNS_REMOVE}/d\" /etc/resolv.conf</action>
				<action type=\"exit\">False exit</action>
			</button>
		</hbox>
	</vbox>
</window>"
	export MAIN_DIALOG
	eval $(gtkdialog --program=MAIN_DIALOG)
	if [ "${EXIT}" == "closewindow" -o "${EXIT}" == "Cancel" -o "${EXIT}" == "abort" ]; then
		exit
	fi
done

#EOF
